/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;

/**
 *
 * @author Kristopher T Babic
 */
public class ToolButton extends Component implements MouseListener {
    private Image image;
    private Dimension sizeImage;
    private boolean status;
    private transient Vector actionListeners = null;
    private String command;
    private int option;
/*
	public ToolButton (String strImage) {
		this(Toolkit.getDefaultToolkit().getImage(strImage));
	}
 */

    public ToolButton(Image image, String command, int option) {
        this.image = image;
        gjt.Util.waitForImage(this, this.image);
        sizeImage = new Dimension(this.image.getWidth(this), this.image.getHeight(this));
        status = false;
        this.command = command;
        this.option = option;
        addMouseListener(this);
    }

    public boolean isIn() {
        return status;
    }

    public void paint(Graphics g) {

        if (status) {
            g.drawImage(image, 3, 3, this);
            paintDown(g);
        }
        else {
            g.drawImage(image, 2, 2, this);
            paintUp(g);
        }
    }

    private void paintUp(Graphics g) {
        g.setColor(Color.white);
        g.drawLine(0, 0, 0, sizeImage.height + 1);
        g.drawLine(1, 0, sizeImage.width + 1, 0);
        g.drawLine(1, 1, 1, sizeImage.height);
        g.drawLine(2, 1, sizeImage.width, 1);

        g.setColor(Color.black);
        g.drawLine(sizeImage.width + 2, 0, sizeImage.width + 2, sizeImage.height + 2);
        g.drawLine(0, sizeImage.height + 2, sizeImage.width + 1, sizeImage.height + 2);

        g.setColor(Color.darkGray);
        g.drawLine(sizeImage.width + 1, 1, sizeImage.width + 1, sizeImage.height + 1);
        g.drawLine(1, sizeImage.height + 1, sizeImage.width, sizeImage.height + 1);
    }

    private void paintDown(Graphics g) {
        g.setColor(Color.black);
        g.drawLine(0, 0, 0, sizeImage.height + 1);
        g.drawLine(1, 0, sizeImage.width + 1, 0);

        g.setColor(Color.darkGray);
        g.drawLine(1, 1, 1, sizeImage.height);
        g.drawLine(2, 1, sizeImage.width, 1);

        g.setColor(Color.white);
        g.drawLine(sizeImage.width + 2, 0, sizeImage.width + 2, sizeImage.height + 2);
        g.drawLine(0, sizeImage.height + 2, sizeImage.width + 1, sizeImage.height + 2);
        g.drawLine(sizeImage.width + 1, 1, sizeImage.width + 1, sizeImage.height + 1);
        g.drawLine(1, sizeImage.height + 1, sizeImage.width, sizeImage.height + 1);
    }

    public void setDown() {
        status = true;
        repaint();

        ToolPanel tPanel = (ToolPanel) getParent();
        Component comps[] = tPanel.getComponents();
        ToolButton button;

        for (int i = 0; i < tPanel.getComponentCount(); i++) {
            if (comps[i] instanceof ToolButton) {
                button = (ToolButton) comps[i];
                if (button != this && button.isIn())
                    button.setUp();
            }
            else if (comps[i] instanceof ToolOptionPanel) {
                ToolOptionPanel top = (ToolOptionPanel) comps[i];
                top.setOption();
            }

        }
    }

    public synchronized void addActionListener(ActionListener l) {
        if (l == null)
            return;
        if (actionListeners == null)
            actionListeners = new Vector();

        actionListeners.add(l);
    }

    public synchronized void removeActionListener(ActionListener l) {
        if (l == null || actionListeners == null || actionListeners.size() <= 0)
            return;

        actionListeners.remove(l);

    }

    protected void processActionEvent(ActionEvent e) {
        if (actionListeners != null) {
            for (int i = 0; i < actionListeners.size(); i++)
                ((ActionListener) actionListeners.elementAt(i)).actionPerformed(e);
        }
    }

    /*
      protected void processEvent(AWTEvent e) {
        if (e instanceof ActionEvent) {
          processActionEvent((ActionEvent)e);
          return;
        }
          super.processEvent(e);
      }
    */
    public void setUp() {
        status = false;
        repaint();
    }

    public Dimension getPreferredSize() {
        return new Dimension(image.getWidth(this) + 4, image.getHeight(this) + 4);
    }

    public Dimension getMinimumSize() {
        return new Dimension(image.getWidth(this) + 4, image.getHeight(this) + 4);
    }

    public void mouseEntered(MouseEvent event) {
    }

    public void mouseExited(MouseEvent event) {
    }

    public void mouseClicked(MouseEvent event) {
    }

    public void mouseReleased(MouseEvent event) {
    }

    public void mousePressed(MouseEvent event) {
        setDown();
        processActionEvent(new ActionEvent(this, option, command));
    }

}

